using UnityEngine;
using System.Collections;

/// \class  gkShootInRhythmPlayerFire
/// \brief 	the player cylindrical movement controller
[AddComponentMenu("GK/ShootInRhythm/Player/gkShootInRhythmPlayerFire")]
[RequireComponent(typeof(gkEventComponent))]
[RequireComponent(typeof(gkShootInRhythmRequestFireInRhythm))]
[RequireComponent(typeof(gkShootInRhythmShipFire))]
public class gkShootInRhythmPlayerFire : MonoBehaviour
{
    /// The event component
    private gkEventComponent m_rEventComponent;
	
	/// Is it possible to fire
	private bool m_bCanFire = true;
	
	/// The Zone Input component
	private gkInputScreenZoneTouch m_rInputScreenZoneTouch;
	
	/// \brief  Called when the component is enabled
	void OnEnable() 
	{
		// Grab event component
		m_rEventComponent = GetComponent<gkEventComponent>();
		
		// Listen the shock event of the owner
		m_rEventComponent.AddEventHandler(gkShootInRhythmFireEvent.StartFire, HandleStartFireEvent);
		
		// Grab the input screen zone touch component
		m_rInputScreenZoneTouch = GetComponent<gkInputScreenZoneTouch>();
	}
	
	/// \brief  Called when the component is disabled
	void OnDisable() 
	{	
		// Remove the shock event of the owner
		m_rEventComponent.RemoveEventHandler(gkShootInRhythmFireEvent.StartFire, HandleStartFireEvent);
	}
	
	/// \brief  Called each frame
    void Update()
    {
		if(m_bCanFire)
		{
		    if(m_rInputScreenZoneTouch.IsZoneTouched())
			{
				m_rEventComponent.RaiseEvent(gkShootInRhythmFireEvent.RequestFireInRhythm);
				m_bCanFire = false;
			}
		}
    }
	
	/// \brief  Handle the event
	void HandleStartFireEvent(Object a_rEvent)
	{
		m_bCanFire = true;
	}
}